import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meco/common/index.dart';
import 'package:meco/views/index.dart';

class MainController extends GetxController {
  MainController();

  // 当前选中的页面索引
  final RxInt currentIndex = 0.obs;

  // 页面控制器
  final PageController pageController = PageController();

  // 页面列表
  late List<Widget> pages;

  // 底部项目
  List<BottomNavItem> bottomNavItems = [
    BottomNavItem(
      icon: IconFontIcons.iconHome,
      label: 'Home',
      route: Routes.home,
    ),
    BottomNavItem(
      icon: IconFontIcons.iconTime,
      label: 'Time',
      route: Routes.time,
    ),
    BottomNavItem(
      icon: IconFontIcons.iconShare,
      label: 'Share',
      route: Routes.share,
    ),
    BottomNavItem(icon: IconFontIcons.iconMe, label: 'Me', route: Routes.me),
  ];

  @override
  void onInit() {
    super.onInit();
    // 初始化页面
    pages = [
      const HomePage(),
      const TimePage(),
      const SharePage(),
      const MePage(),
    ];
  }

  // 切换页面
  void changePage(int index) {
    currentIndex.value = index;
    pageController.jumpToPage(index);
    update(["main"]);
  }

  void onTap() {}

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  _initData() {
    update(["main"]);
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }
}

// 底部导航项
class BottomNavItem {
  final IconData icon;
  final String label;
  final String route;

  BottomNavItem({required this.icon, required this.label, required this.route});
}
